package com.byh.singer.service.impl;

import com.byh.singer.dao.CustomerMapper;
import com.byh.singer.dao.ProductMapper;
import com.byh.singer.dao.ReviewMapper;
import com.byh.singer.pojo.*;
import com.byh.singer.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {
    private static final Logger logger = LoggerFactory.getLogger(ReviewServiceImpl.class);

    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Review> list() {
        // 获取评论列表
        List<Review> reviews = reviewMapper.selectByExample(null);
        // 如果 reviews 为 null，返回一个空列表
        if (reviews == null) {
            return new ArrayList<>(); // 返回空列表以避免 NullPointerException
        }

        // 遍历评论列表
        for (Review review : reviews) {
            // 检查 review 是否为 null
            if (review == null) {
                continue; // 跳过 null 元素
            }

            // 检查 cstid 是否为 null
            Integer cstid = review.getCstid();
            if (cstid != null) {
                Customer customer = customerMapper.selectByPrimaryKey(cstid);
                review.setCustomer(customer);
            }

            // 检查 pid 是否为 null
            Integer pid = review.getPid();
            if (pid != null) {
                Product product = productMapper.selectByPrimaryKey(pid);
                review.setProduct(product);
            }
        }
        return reviews; // 返回处理后的评论列表
    }

//
//    @Override
//    public List<Review> list() {
//        List<Review> reviews = reviewMapper.selectByExample(null);
//        for (Review review:reviews){
//            Customer customer = customerMapper.selectByPrimaryKey(review.getCstid());
//            Product product = productMapper.selectByPrimaryKey(review.getPid());
//            review.setCustomer(customer);
//            review.setProduct(product);
//        }
//        return reviews;
//    }


    @Override
    public List<Review> getReviewListByPid(int id) {
        // 创建查询条件
        ReviewExample example = new ReviewExample();
        example.createCriteria().andPidEqualTo(id);

        // 查询 Review 列表
        List<Review> reviews = reviewMapper.selectByExample(example);
        if (reviews == null || reviews.isEmpty()) {
            return Collections.emptyList(); // 返回空列表避免 null
        }

        // 遍历每个 Review，查询其 Customer 并设置
        for (Review review : reviews) {
            if (review != null && review.getCstid() != null) { // 判断 review 和 cstid 是否为 null
                Customer customer = customerMapper.selectByPrimaryKey(review.getCstid());
                review.setCustomer(customer); // 即使 customer 为 null，也不会抛异常
            } else {
                logger.warn("未找到商品id: " + id);
            }
        }

        return reviews;
    }


//    @Override
//    public List<Review> getReviewListByPid(int id) {
//        ReviewExample example = new ReviewExample();
//        example.createCriteria().andPidEqualTo(id);
//        List<Review> reviews = reviewMapper.selectByExample(example);
//        for (Review review:reviews){
//            Customer customer = customerMapper.selectByPrimaryKey(review.getCstid());
//            review.setCustomer(customer);
//        }
//
//        return reviews;
//    }

    @Override
    public void del(int id) {
        reviewMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Review get(int id) {
        return reviewMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Review review) {
        reviewMapper.insert(review);
    }
}
